package com.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.annotation.AuthCheck;
import com.study.common.BaseResponse;
import com.study.common.DeleteRequest;
import com.study.common.ResultUtils;
import com.study.constant.UserConstant;
import com.study.exception.ErrorCode;
import com.study.exception.ThrowUtils;
import com.study.model.dto.post.PostAddRequest;
import com.study.model.dto.post.PostQueryRequest;
import com.study.model.dto.post.PostUpdateRequest;
import com.study.model.dto.task.TaskAddRequest;
import com.study.model.dto.task.TaskQueryRequest;
import com.study.model.dto.task.TaskUpdateRequest;
import com.study.model.entity.Post;
import com.study.model.entity.Task;
import com.study.model.vo.PostVO;
import com.study.service.PostService;
import com.study.service.TaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 每日计划控制器
 */
@RestController
@RequestMapping("/task")
public class TaskController {
    @Resource
    private PostService postService;
    @Resource
    private TaskService taskService;

    /**
     * 创建计划
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTask(@RequestBody TaskAddRequest taskAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(taskAddRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(taskService.createTask(taskAddRequest,request));
    }





    /**
     * 删除计划（整日计划）
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTask(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(taskService.removeById(deleteRequest.getId()));

    }

    /**
     * 更新计划
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTask(@RequestBody TaskUpdateRequest taskUpdateRequest) {
        ThrowUtils.throwIf(taskUpdateRequest == null || taskUpdateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(taskService.updateTask(taskUpdateRequest));

    }

    /**
     * 根据userId查询用户计划
     *
     * @param request 查询请求参数
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<List<Task>> listTaskByUserId(HttpServletRequest request) {
        return ResultUtils.success(taskService.listTaskByUserId(request));
    }
}
